package com.xbongbong.jxc.abstracts;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.business.rule.FormDataActiveRuleHelp;
import com.xbongbong.paas.data.service.CabinetFileService;
import com.xbongbong.paas.data.service.RichTextService;
import com.xbongbong.paas.data.service.SerialNoService;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.lowcode.LowCodeExecuteHelp;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.parent.abstracts.AbstractFormDataService;
import com.xbongbong.pro.formdata.pojo.vo.BeforeSaveVO;
import com.xbongbong.pro.formdata.pojo.vo.SaveVO;
import com.xbongbong.process.operate.ProcessFieldHelp;
import com.xbongbong.process.operate.ProcessStartHelp;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.paas.service.workflow.trigger.WorkflowFormDataOperationService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 进销存抽象类，实现进销存中的公用方法
 * @author 吴峰
 * @date 2022/01/05 10:12
 */
@Component
public abstract class AbstractJxcAddDataService extends AbstractFormDataService {

    @Resource
    private ProcessStartHelp processStartHelp;
    @Resource
    private WorkflowFormDataOperationService workflowFormDataOperationService;
    @Resource
    private ProcessFieldHelp processFieldHelp;
    @Resource
    private LowCodeExecuteHelp lowCodeExecuteHelp;
    @Resource
    private FormDataActiveRuleHelp formDataActiveRuleHelp;
    @Resource
    private CabinetFileService cabinetFileService;
    @Resource
    private SerialNoService serialNoService;
    @Resource
    private RichTextService richTextService;

    /**
     * 保存富文本
     *
     * @param formDataAddDTO
     * @param paasFormDataEntity
     * @param richTextData 富文本数据
     * @throws XbbException
     */
    protected void saveRichText(FormDataAddDTO formDataAddDTO, PaasFormDataEntity paasFormDataEntity,Object richTextData) throws XbbException{
        richTextService.saveFormRichText(formDataAddDTO, paasFormDataEntity, richTextData);
    }

    /**
     * 移除富文本
     *
     * @param data Json数据
     * @return 富文本数据
     * @throws XbbException
     */
    protected Object removeRichText(JSONObject data) throws XbbException{
        return FormDataUtil.removeRichText(data);
    }

    @Override
    public PaasFormDataEntity getFormData(FormDataAddDTO formDataSaveDTO, PaasFormEntityExt paasFormEntityExt, Map<String, FieldAttrEntity> explainMap, Long department, JSONObject data) throws XbbException {
        // 获取流水号
        PaasFormDataEntity paasFormDataEntity = serialNoService.setSerialNo(formDataSaveDTO, formDataSaveDTO.getCorpid(), formDataSaveDTO.getBusinessType(), explainMap);
        paasFormDataEntity.setCorpid(formDataSaveDTO.getCorpid());
        paasFormDataEntity.setAppId(paasFormEntityExt.getAppId());
        paasFormDataEntity.setMenuId(paasFormEntityExt.getMenuId());
        paasFormDataEntity.setFormId(paasFormEntityExt.getId());
        String ownerId = formDataSaveDTO.getUserId();
        paasFormDataEntity.setOwnerId(ownerId);
        // 所属部门的id，由前端传入 所属部门不可见，前端拿不到时，卡特建议直接赋值-1
        Long departmentId = department == null ? -1L : department;
        paasFormDataEntity.setDepartmentId(departmentId);
        formDataSaveDTO.setDepartmentId(departmentId);
        if ( Objects.nonNull(formDataSaveDTO.getOwnerId()) && !formDataSaveDTO.getOwnerId().isEmpty() ) {
            paasFormDataEntity.setOwnerId(Objects.toString(formDataSaveDTO.getOwnerId().get(0).getId(), ""));
        }
        paasFormDataEntity.setSerialNo(formDataSaveDTO.getSerialNo());
        paasFormDataEntity.setFlowStatus(FlowStatusEnum.NOT_NEED_APPROVAL.getType());

        paasFormDataEntity.setData(data);
        paasFormDataEntity.setCreatorId(formDataSaveDTO.getUserId());
        if ( StringUtil.isNotEmpty(FastJsonHelper.getStringFromFormData(formDataSaveDTO.getDataList(), FieldTypeEnum.CREATORID.getAlias())) ) {
            paasFormDataEntity.setCreatorId(FastJsonHelper.getStringFromFormData(formDataSaveDTO.getDataList(), FieldTypeEnum.CREATORID.getAlias()));
            formDataSaveDTO.getDataList().remove(FieldTypeEnum.CREATORID.getAlias());
        }
        return paasFormDataEntity;
    }

    @Override
    public boolean isEnableNewWorkflow(FormDataAddDTO formDataSaveDTO, BeforeSaveVO beforeSaveVO, PaasFormDataEntity paasFormDataEntity) throws XbbException {
        return workflowFormDataOperationService.isEnableNewWorkflow(formDataSaveDTO, beforeSaveVO, paasFormDataEntity);
    }

    @Override
    public Integer startProcess(FormDataAddDTO formDataAddDTO, BeforeSaveVO beforeSaveVO) throws XbbException {
        return processStartHelp.startProcess(formDataAddDTO, beforeSaveVO);
    }

    @Override
    public Map<String, FieldAttrEntity> processSetVisible(PaasFormEntityExt paasFormEntityExt, FormDataAddDTO formDataSaveDTO, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        return processFieldHelp.processSetVisible(paasFormEntityExt.getIsProcessForm(), paasFormEntityExt.getId(), paasFormEntityExt.getAppId(), formDataSaveDTO, explainMap);
    }

    @Override
    public Map<String, FieldAttrEntity> importProcessSetVisible(PaasFormEntityExt paasFormEntityExt, FormDataAddDTO formDataSaveDTO, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        return processFieldHelp.importProcessSetVisible(paasFormEntityExt.getIsProcessForm(), paasFormEntityExt.getId(), paasFormEntityExt.getAppId(), formDataSaveDTO, explainMap);
    }

    /**
     * 低代码执行服务
     * @param formDataAddDTO
     * @param saveVO
     * @param beforeSaveVO
     * @throws XbbException
     */
    @Override
    public void lowCodeExecuteService(FormDataAddDTO formDataAddDTO, SaveVO saveVO, BeforeSaveVO beforeSaveVO) throws XbbException {
        lowCodeExecuteHelp.lowCodeExecuteService(formDataAddDTO, saveVO, beforeSaveVO);

    }


    @Override
    public void activeRule(FormDataAddDTO formDataAddDTO, BeforeSaveVO beforeSaveVO, PaasFormDataEntity newPaasFormDataEntity, PaasFormEntityExt paasFormEntityExt) throws XbbException {
        formDataActiveRuleHelp.activeRule(formDataAddDTO, beforeSaveVO, newPaasFormDataEntity, paasFormEntityExt);
    }
    @Override
    public void workflowAddTrigger(FormDataAddDTO formDataAddDTO, BeforeSaveVO beforeSaveVO, FormDataAddVO formDataSaveVO) throws XbbException {
        workflowFormDataOperationService.workflowAddTrigger(formDataAddDTO, beforeSaveVO, formDataSaveVO);
    }

    /**
     * 保存文件柜
     * @param formDataAddDTO
     * @param paasFormDataEntity
     * @param userVO
     * @throws XbbException
     */
    public void saveFile(FormDataAddDTO formDataAddDTO, PaasFormDataEntity paasFormDataEntity, UserVO userVO) throws XbbException{
        String corpid = formDataAddDTO.getCorpid();
        Long dataId = paasFormDataEntity.getDataId();
        Integer businessType = formDataAddDTO.getBusinessType();
        //文件柜数据保存
        cabinetFileService.saveDataCabinetFile(formDataAddDTO.getExplainMap(), paasFormDataEntity, corpid, dataId, businessType, userVO.getUserId());

    }
}
